﻿using Learun.Util;
using Learun.Util.SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;

namespace Learun.Application.WorkFlow
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2020-08-24 15:42
    /// 描 述：模型提资
    /// </summary>
    public class CapitalRaisingService
    {
        #region 仓储
        Repository<ZSJT_BIMCD_CapitalRaisingEntity> _capitalRaisingRepository => new Repository<ZSJT_BIMCD_CapitalRaisingEntity>();
        #endregion
        #region 构造函数和属性

        private string fieldSql;
        public CapitalRaisingService()
        {
            fieldSql=@"
                t.CapitalRaisingID,
                t.ProcessId,
                t.ModelFileID,
                t.ApprovalReMark,
                t.CreateUserID,
                t.CreateTime,
                t.UpdateUserID,
                t.UpdateTime,
                t.CapitalRaisingMajorCode,
                t.ReviewMajorCode,
                t.CapitalRaisingRole
            ";
        }
        #endregion

        #region 获取数据

        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ZSJT_BIMCD_CapitalRaisingEntity> GetList( string queryJson )
        {
            try
            {
                //参考写法
                //var queryParam = queryJson.ToJObject();
                // 虚拟参数
                //var dp = new DynamicParameters(new { });
                //dp.Add("startTime", queryParam["StartTime"].ToDate(), DbType.DateTime);
                var strSql = new StringBuilder();
                strSql.Append("SELECT ");
                strSql.Append(fieldSql);
                strSql.Append(" FROM ZSJT_BIMCD_CapitalRaising t ");
                //return this.BaseRepository().FindList<ZSJT_BIMCD_CapitalRaisingEntity>(strSql.ToString());
                return SqlSugarHelper.Db.SqlQueryable<ZSJT_BIMCD_CapitalRaisingEntity>(strSql.ToString()).ToList();
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 获取列表分页数据
        /// <param name="pagination">分页参数</param>
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ZSJT_BIMCD_CapitalRaisingEntity> GetPageList(Pagination pagination, string queryJson)
        {
            try
            {
                var strSql = new StringBuilder();
                strSql.Append("SELECT ");
                strSql.Append("t.*");
                strSql.Append(" FROM ZSJT_BIMCD_CapitalRaising t ");
                //return this.BaseRepository().FindList<ZSJT_BIMCD_CapitalRaisingEntity>(strSql.ToString(), pagination);
                return SqlSugarHelper.Db.SqlQueryable<ZSJT_BIMCD_CapitalRaisingEntity>(strSql.ToString()).ToPageList(pagination.page, pagination.rows);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ZSJT_BIMCD_CapitalRaisingEntity GetEntity(string keyValue)
        {
            try
            {
                return _capitalRaisingRepository.GetById(keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }
        /// <summary>
        /// 获取主表实体数据
        /// <param name="processId">流程实例ID</param>
        /// <summary>
        /// <returns></returns>
        public ZSJT_BIMCD_CapitalRaisingEntity GetEntityByProcessId(string processId)
        {
            try
            {
                var strSql = new StringBuilder();
                strSql.Append(@"
 SELECT t.*,n.F_IsFinished AS IsFinished,c.AttrTypeName AS CapitalRaisingMajorName,t2.LayerID,t2.ModelFilePath,t2.ModelFileName,n.ProjectId,p.ProjectName
,STUFF((SELECT '\' + t3.ModelCatalogName FROM ZSJT_BIMCD_ModelCatalog  t3 WHERE  
 t3.ModelCatalogID IN ( SELECT * FROM dbo.f_splitstr((SELECT t4.FullPath FROM ZSJT_BIMCD_ModelCatalog t4 WHERE t4.ModelCatalogID = t2.ModelCatalogID),',')) 
 ORDER BY FullPath FOR XML PATH('')),1,1,'')  AS FullPath 
  FROM ZSJT_BIMCD_CapitalRaising t 
  LEFT JOIN   ZSJT_BIMCD_AttrTypeCatalog c ON t.CapitalRaisingMajorCode = c.FullCode  
    LEFT JOIN   LR_NWF_Process n ON t.ProcessId = n.F_Id  
  LEFT JOIN    ZSJT_BIMCD_Project p  ON n.ProjectId = p.ProjectId  
   LEFT JOIN ZSJT_BIMCD_ModelFile t2  ON t.ModelFileID = t2.ModelFileID  
  LEFT JOIN ZSJT_BIMCD_ModelCatalog mc ON mc.ModelCatalogID = t2.ModelCatalogID");
                strSql.Append(" WHERE t.ProcessId= @processId");
                return SqlSugarHelper.Db.Ado.SqlQuerySingle<ZSJT_BIMCD_CapitalRaisingEntity>(strSql.ToString(), new { processId });
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 验证 获取当前专业和当前模型的流程完成的数据
        /// </summary>
        /// <param name="majorCode">专业</param>
        /// <param name="modelFileId">模型Id</param>
        /// <returns></returns>
        public IEnumerable<ZSJT_BIMCD_CapitalRaisingEntity> TrialTypeValid(string majorCode, string modelFileId)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT t1.* ");
            strSql.Append(" FROM ZSJT_BIMCD_CapitalRaising t1 ");
            strSql.Append(" WHERE T1.ProcessId IN (SELECT F_Id FROM LR_NWF_Process WHERE F_EnabledMark=1  and F_IsFinished =0 )");
            strSql.Append("AND t1.ModelFileID=@ModelFileID AND t1.CapitalRaisingMajorCode=@MajorCode");
            //var list = (List<ZSJT_BIMCD_CapitalRaisingEntity>)this.BaseRepository().FindList<ZSJT_BIMCD_CapitalRaisingEntity>(strSql.ToString(), new { ModelFileID = modelFileId, MajorCode = majorCode });
            var list = SqlSugarHelper.Db.SqlQueryable<ZSJT_BIMCD_CapitalRaisingEntity>(strSql.ToString()).AddParameters(new { ModelFileID = modelFileId, MajorCode = majorCode }).ToList();
            return list;
        }
        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string keyValue)
        {
            try
            {
                _capitalRaisingRepository.Delete(t=>t.CapitalRaisingID == keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string keyValue, ZSJT_BIMCD_CapitalRaisingEntity entity)
        {
            try
            {
                if (!string.IsNullOrEmpty(keyValue) && GetEntity(keyValue) != null)
                {
                    entity.Modify(keyValue);
                    _capitalRaisingRepository.Update(entity);
                }
                else
                {
                    _capitalRaisingRepository.Insert(entity);
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

    }
}
